<template>
  <div class="app-container">
    <el-form
      :model="menu"
      :rules="rules"
      ref="ruleForm"
      label-width="200px"
      class="demo-ruleForm"
    >
      <el-form-item label="显示名" prop="name">
        <el-input v-model="menu.nam"></el-input>
      </el-form-item>
      <el-form-item label="链接URL" prop="url">
        <el-input v-model="menu.url"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="menu.remarks"></el-input>
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNum">
        <el-input-number
          v-model="menu.orderNum"
          :min="1"
          :max="999"
          label="排序号"
        ></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateMenuApi } from "@/api/menu";
import { getMenuApi } from "@/api/menu";
export default {
  data() {
    return {
      menu: {},
      rules: {
        menuUrl: [
          { required: true, message: "请输入Menu链接URL", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "请输入排序号", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.menu.id = this.$route.params.id;
    this.getMenu(this.menu.id);
  },
  methods: {
    getMenu(id) {
      getMenuApi(id).then((response) => {
        this.menu = response.data;
        delete this.menu.createTime;
      });
    },
    updateMenu(form) {
      updateMenuApi(form).then((res) => {
        if (res.status == 0) {
          this.$message("提交成功");
        }
        this.$router.push("/content/topmenu");
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var formData = new FormData();
          Object.keys(this.menu).forEach((key) => {
            formData.append(key, this.menu[key]);
          });
          this.updateMenu(formData);
        }
      });
    },
  },
};
</script>